package com.yuexiang.melodyhub.authserver.controller;

import com.yuexiang.melodyhub.authserver.dto.OAuthLogout.param.OAuthLogoutParam;
import com.yuexiang.melodyhub.authserver.feign.UserFeignService;
import com.yuexiang.melodyhub.common.constant.UsualConstant.BizConstant;
import com.yuexiang.melodyhub.common.exception.gateway.UnavailableServiceException;
import com.yuexiang.melodyhub.common.result.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/oauth")
public class LogoutController {

    @Autowired
    private RedisTemplate<String, String> restTemplate;

    @Autowired
    private UserFeignService userFeignService;

    @PostMapping("/logout")
    public ResultData<?> logout(@RequestBody OAuthLogoutParam param) {
        try {
            userFeignService.saveLastTimeByEmail(param.getEmail());
        } catch (Exception e) {
            throw new UnavailableServiceException();
        }
        restTemplate.delete(BizConstant.TOKEN_CACHE_PREFIX + param.getUsername());
        return ResultData.success(null);
    }
}
